package com.stevenmoon.csdnplus.presenter;

import com.stevenmoon.csdnplus.app.Constants;
import com.stevenmoon.csdnplus.bean.headline.HeadlineInfoBean;
import com.stevenmoon.csdnplus.bean.headline.LatestNewsReqBean;
import com.stevenmoon.csdnplus.fragment.HeadlineLatestListFragment;
import com.stevenmoon.csdnplus.logic.HeadlineManager;
import com.stevenmoon.csdnplus.presenter.base.MvpBaseNoCheckPresenter;

import java.util.List;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by WangJun on 2016/4/12.
 */
public class HeadlineLatestPresenter extends MvpBaseNoCheckPresenter<HeadlineLatestListFragment> {


    public void loadNewData(){
        getView().showLoading();
        LatestNewsReqBean reqBean = new LatestNewsReqBean();
        reqBean.setLastId("-");
        reqBean.setDirection(Constants.CONFIG.DIRECTION_DOWN);
        Subscription s = HeadlineManager.getLatestNews(reqBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<HeadlineInfoBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().hideLoading();
                        getView().loadNewDataFail(e.getMessage());
                    }

                    @Override
                    public void onNext(List<HeadlineInfoBean> headlineInfoBeen) {
                        getView().hideLoading();
                        //TODO 将新数据缓存到本地数据库
                        getView().loadNewDataSuccess(headlineInfoBeen);
                    }
                });

        getCompositeSubscription().add(s);
        
    }

    public void loadMoreData(String lastId){
        LatestNewsReqBean reqBean = new LatestNewsReqBean();
        reqBean.setLastId(lastId);
        reqBean.setDirection(Constants.CONFIG.DIRECTION_UP);
        Subscription s = HeadlineManager.getLatestNews(reqBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<HeadlineInfoBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().loadMoreDataFail(e.getMessage());
                    }

                    @Override
                    public void onNext(List<HeadlineInfoBean> headlineInfoBeen) {
                        if(headlineInfoBeen == null || headlineInfoBeen.isEmpty()){
                            getView().showNoMoreData();
                        }else{
                            getView().loadMoreDataSuccess(headlineInfoBeen);
                        }

                    }
                });

        getCompositeSubscription().add(s);
    }


}
